package pe.gob.mintra.trabajadoreshogar.daoimpl;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import pe.gob.mintra.trabajadoreshogar.action.CalculoCtsAction;
import pe.gob.mintra.trabajadoreshogar.bean.Cts;
import pe.gob.mintra.trabajadoreshogar.bean.Empleador;
import pe.gob.mintra.trabajadoreshogar.bean.Liquidacion;
import pe.gob.mintra.trabajadoreshogar.dao.CtsDAO;
import pe.gob.mintra.trabajadoreshogar.factory.MyBatisConnectionFactory;

public class CtsDAOImp implements CtsDAO {
	
	private final static Logger logCtsDAOImp = Logger.getLogger(CtsDAOImp.class);
	
	private SqlSessionFactory sqlSessionFactory;

	public CtsDAOImp() {
		sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
	}
	

	@Override
	public void insertarCts(Cts objCts){
		
		SqlSession session = sqlSessionFactory.openSession();

		try {
			
			session.insert("Cts.insertarCts",objCts);
		} 
		catch(Exception ex){
			session.rollback();
			logCtsDAOImp.error(ex.getMessage());
		}
		finally {
			session.close();
		}

	}


	@Override
	public Cts obtenerUltimaCts(Liquidacion objLiquidacion) {
		SqlSession session = sqlSessionFactory.openSession();
		Cts ultimaCts=null;
		
		try{
			ultimaCts=(Cts)session.selectOne("Cts.obtenerUltimaCts",objLiquidacion);
		}catch(Exception ex){
			
		}finally{
			session.close();
		}
		
		return ultimaCts;
	}


	@Override
	public Cts obtenerCts(Cts objCts) {
		SqlSession session = sqlSessionFactory.openSession();
		Cts cts=null;
		
		try{
			cts=(Cts)session.selectOne("Cts.obtenerCts",objCts);
		}catch(Exception ex){
			
		}finally{
			session.close();
		}
		
		return cts;
	}


	@Override
	public void actualizarCts(Cts objCts) {
		SqlSession session = sqlSessionFactory.openSession();

		try {
			
			session.update("Cts.actualizarCts",objCts);
		} 
		catch(Exception ex){
			session.rollback();
			logCtsDAOImp.error(ex.getMessage());
		}
		finally {
			session.close();
		}
		
	}

}
